package com.example.rabbitmq.work;

import com.example.rabbitmq.constant.Constants;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setUsername(Constants.USERNAME);
        factory.setPassword(Constants.PASSWORD);
        factory.setVirtualHost(Constants.VIRTUALHOST);
        //创建连接
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(Constants.WORK_QUEUE, true, false, false, null);
        //发送消息
        for (int i = 0; i < 10; i++) {
            String message = "hello rabbitmq" + i;
            channel.basicPublish("", Constants.WORK_QUEUE, null, message.getBytes());
        }
        System.out.println("消息发送成功");
        channel.close();
        connection.close();
    }
}
